import 'package:concierge/app/routes/app_routes.dart';
import 'package:concierge/app/widgets/ms_svg_icon.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class MSAppBar extends StatefulWidget implements PreferredSizeWidget {
  MSAppBar({
    super.key,
    this.title,
    this.backgroundColor,
    this.foregroundColor,
    this.actions,
    this.leading,
    double? leadingWidth,
    this.centerTitle = true,
    double? toolbarHeight,
    this.titleSpacing = 0,
    this.elevation = 0,
    this.onBack,
    this.systemOverlayStyle,
  })  : toolbarHeight = toolbarHeight ?? 44.h,
        leadingWidth = leadingWidth ?? 56.w;

  final dynamic title;
  final Color? backgroundColor;
  final Color? foregroundColor;
  final List<Widget>? actions;
  final Widget? leading;
  final double? leadingWidth;
  final bool? centerTitle;
  final double? toolbarHeight;
  final double? titleSpacing;
  final double? elevation;
  final SystemUiOverlayStyle? systemOverlayStyle;
  final VoidCallback? onBack;

  @override
  State<MSAppBar> createState() => _MSAppBarState();

  @override
  Size get preferredSize => Size.fromHeight(toolbarHeight!);
}

class _MSAppBarState extends State<MSAppBar> {
  @override
  void initState() {
    super.initState();
  }

  bool get isBackHome => [Routes.index].contains(Get.routing.current);
  bool get isLoginPage => Routes.login == Get.routing.current;
  bool get isHomePage => Routes.index == Get.routing.current;

  Widget? _buildLeading(BuildContext context) {
    final colorScheme = context.theme.colorScheme;
    if (!isLoginPage && !isHomePage && widget.leading == null) {
      return IconButton(
        onPressed: () {
          if (widget.onBack != null) {
            widget.onBack!.call();
          } else {
            Get.back();
          }
        },
        style: IconButton.styleFrom(
          foregroundColor: widget.foregroundColor ??
              (isBackHome ? colorScheme.onSecondary : colorScheme.primary),
        ),
        icon: MSSvgIcon(isBackHome ? 'home' : 'chevron_left', size: 24.w),
      );
    }

    return widget.leading;
  }

  Widget? _buildTitle(BuildContext context) {
    final textTheme = context.textTheme;

    if (widget.title is String) {
      return Text(
        (widget.title ?? '').toUpperCase(),
        style: textTheme.headlineMedium?.copyWith(
          color: widget.foregroundColor,
        ),
      );
    }
    return widget.title;
  }

  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: _buildTitle(context),
      backgroundColor: widget.backgroundColor,
      foregroundColor: widget.foregroundColor,
      actions: widget.actions,
      shadowColor: Colors.transparent,
      elevation: widget.elevation,
      systemOverlayStyle: widget.systemOverlayStyle,
      leading: Container(
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.only(left: 3.w),
        child: _buildLeading(context),
      ),
      leadingWidth: widget.leadingWidth,
      centerTitle: widget.centerTitle,
      toolbarHeight: widget.toolbarHeight,
      titleSpacing: widget.titleSpacing,
      titleTextStyle: context.textTheme.headlineMedium,
      forceMaterialTransparency: true,
    );
  }
}
